package com.su.gulimall.member.interceptor;

import com.su.common.vo.MemberRespVo;
import org.springframework.stereotype.Component;
import org.springframework.util.AntPathMatcher;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
 public class MyInterceptor implements HandlerInterceptor {
    public static ThreadLocal<MemberRespVo> loginUser=new ThreadLocal<>();
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String uri = request.getRequestURI();
        boolean match = new AntPathMatcher().match("/order/order/getOrderStatus/**", uri);
        boolean match1 = new AntPathMatcher().match("/member/**", uri);
        if(match||match1){
            return true;
        }
        MemberRespVo attribute = (MemberRespVo) request.getSession().getAttribute("loginUser");
        if(attribute!=null)
        {
            //已经登录的用户,将其信息放入ThreadLocal，同一请求共享该数据
            loginUser.set(attribute);
            return true;
        }else {
            request.getSession().setAttribute("msg","请先登录");
            response.sendRedirect("http://auth.gulimall.com");
            return false;
        }
    }
}
